package com.hydl.power.controller;

import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.*;
import com.hydl.power.service.*;
import com.hydl.power.utils.Resp;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/loopnav")
@Api(value = "LoopNavController", tags = "回路导航")
@CrossOrigin
public class LoopNavController {


    @Autowired
    private CityService cityService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private PowerRoomService powerRoomService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TemperatureDeviceService temperatureDeviceService;

    // @LogAnnotation(module = "回路导航",description = "查询城市")
    @PostMapping("/queryCity")
    @ApiOperation(value="查询城市")
    public Resp queryCityByToken(String token){
        SysUser sysUser = sysUserService.getUserByToken(token);
        List<City> list = new ArrayList<>();
        if(sysUser != null){
          if(sysUser.getStatus() == 2 ){
            System.out.println(String.valueOf(sysUser.getProjectId()));
             list=cityService.queryCityByToken(sysUser.getProjectId().toString(),token);
          }else {
            list = cityService.queryCity();
          }
        }
      if(list.isEmpty()){
        return Resp.success("未查询到数据",null);
      }
      return Resp.success(list);
    }


    //@LogAnnotation(module = "回路导航",description = "查询城市")
    @PostMapping("/queryProjectAll")
    @ApiOperation(value="查询项目")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",required = true,  dataType = "String",defaultValue = "2O8QDw00Y1")
    })
    public Resp<List<Project>> queryProjectAll(String token){
        return Resp.success(projectService.queryProjectAll(token));
    }


    // @LogAnnotation(module = "回路导航",description = "查询城市")
    @PostMapping("/queryLongitudeLatitudeAll")
    @ApiOperation(value="查询所有经纬度")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",required = true,  dataType = "String",defaultValue = "2O8QDw00Y1")
    })
    public Resp queryLongitudeLatitudeAll(String token){
        SysUser sysUser = sysUserService.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            Project project = projectService.queryProjectByProId(String.valueOf(sysUser.getProjectId()));
            if(project==null){
                return Resp.success("未查询到数据",null);
            }
/*            List arr=new ArrayList<>();
            List arr2=new ArrayList<>();
            double Longitude = Double.valueOf(project.getLongitudeLatitude())  ;
            double Latitude =Double.valueOf(project.getLatitude_latitude());
            arr.add(Longitude);
            arr.add(Latitude);
            project.setList(arr);
            arr2.add(project);*/
            return Resp.success(project);
        }
        List<Project> list =projectService.queryProjectAll(token);
        if(list.isEmpty()){
            Resp.fail("为查询到数据");
        }
/*        List arr=new ArrayList<>();
        List arr2=new ArrayList<>();
        for (Project project : list) {
            double Longitude = Double.valueOf(project.getLongitudeLatitude())  ;
            double Latitude =Double.valueOf(project.getLatitude_latitude());
            arr.add(Longitude);
            arr.add(Latitude);
            arr2.add(arr);
            arr=new ArrayList<>();
        }*/
        return Resp.success(list);
    }



    //@LogAnnotation(module = "回路导航",description = "查询城市")
    @PostMapping("/queryProject")
    @ApiOperation(value="查询所在城市的项目")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id",required = true,  dataType = "String",defaultValue = "0371"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",required = true,  dataType = "String",defaultValue = "2O8QDw00Y1"),
    })
    public Resp queryCity(String cityId,String token){
        SysUser sysUser = sysUserService.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            List<Project> list=projectService.queryProjectByToken(cityId,token);
            if(list.isEmpty()){
                return Resp.success("未查询到数据",null);
            }
            return Resp.success(list);
        }
        List<Project> pro=projectService.queryProjectId(cityId);
        if(pro.isEmpty()){
            return  Resp.success("该城市暂未开发项目",null);
        }
        return Resp.success(pro);
    }



    // @LogAnnotation(module = "回路导航",description = "查询城市")
    @PostMapping("/queryPowerroom")
    @ApiOperation(value="查询项目下配电房")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",required = true,  dataType = "String",defaultValue = "50642475"),
    })
    public Resp<List<PowerRoom>> queryPowerroom(String projectId){
       /* SysUser sysUser = sysUserService.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            List<PowerRoom> list=powerRoomService.queryPowerroomByToken(projectId);
            if(list.isEmpty()){
                return Resp.success("未查询到数据",null);
            }
            return Resp.success(list);
        }*/
        if(powerRoomService.queryPowerroom(projectId).isEmpty()){
            return  Resp.success("该项目下暂未配置配电房",null);
        }
        return Resp.success(powerRoomService.queryPowerroom(projectId));
    }

    // @LogAnnotation(module = "回路导航",description = "查询配电房下测温设备")
    @PostMapping("/getTemperatureDevice")
    @ApiOperation(value="查询配电房下测温设备")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id",required = true,  dataType = "String",defaultValue = "70980664")
    })
    public Resp<List<TemperatureDevice>> getTemperatureDevice(String roomId){
        List<TemperatureDevice> list=temperatureDeviceService.getTemperatureDeviceByRoomId(roomId);
        return Resp.success(list);
    }


    //@LogAnnotation(module = "回路导航",description = "查询配电房下设备")
    @PostMapping("/queryDevice")
    @ApiOperation(value="查询配电房下设备")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id",required = true,  dataType = "String",defaultValue = "70980664")
    })
    public Resp<List<Device>> queryDevice(String roomId){
        if(deviceService.queryDevice(roomId).isEmpty()){
            return  Resp.success("该配电房暂无设备",null);
        }
        return Resp.success(deviceService.queryDevice(roomId));
    }


    @LogAnnotation(module = "回路导航",description = "根据Id删除项目")
    @PostMapping("/deleteProjectById")
    @ApiOperation(value="根据Id删除项目")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "项目自增长id",required = false,  dataType = "Integer",defaultValue = "3")
    })
    public Resp deleteProjectById(Integer id){
        Project project=projectService.queryProjectById(id);
        int count=powerRoomService.selectCountByProjectId(project.getProjectId());
        if(count>0){
            return Resp.fail("项目下还有配电房未删除");
        }
        int flag = projectService.deleteProjectById(id);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("删除失败");
        }

    }

    //@LogAnnotation(module = "回路导航",description = "添加项目")
    @PostMapping("/addProject")
    @ApiOperation(value="添加项目")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id",required = false,  dataType = "String",defaultValue = "0372"),
            @ApiImplicitParam(paramType = "query", name = "projectName", value = "项目名称",required = false,  dataType = "String",defaultValue = "京东商城"),
            @ApiImplicitParam(paramType = "query", name = "longitudeLatitude", value = "经度",required = false,  dataType = "String",defaultValue = "113.9999"),
            @ApiImplicitParam(paramType = "query", name = "latitudeLatitude", value = "纬度",required = false,  dataType = "String",defaultValue = "114.3333"),
            @ApiImplicitParam(paramType = "query", name = "createTableStatus", value = "是否添加项目数据表 1是 2否",required = false,  dataType = "String",defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "createTemperatureDataTableStatus", value = "是否添加电气火灾数据表 1是 2否",required = false,  dataType = "String",defaultValue = "1"),

    })
    public Resp addProject(String cityId,String projectName, String longitudeLatitude,String latitudeLatitude,String createTableStatus,String createTemperatureDataTableStatus){
            if(StringUtils.isBlank(cityId) || StringUtils.isBlank(projectName) || StringUtils.isBlank(longitudeLatitude) || StringUtils.isBlank(latitudeLatitude) || StringUtils.isBlank(createTableStatus)){
                return Resp.fail("参数不能为空");
        }
        int flag = projectService.addProject(cityId,projectName,longitudeLatitude,latitudeLatitude,createTableStatus,createTemperatureDataTableStatus);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("添加失败");
        }
    }


    @LogAnnotation(module = "回路导航",description = "修改项目")
    @PostMapping("/updateProjec")
    @ApiOperation(value="修改项目")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectName", value = "项目名称",required = true,  dataType = "String",defaultValue = "京东商城"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "id",required = false,  dataType = "Integer",defaultValue = "27")
    })
    public Resp updateProjec(String projectName,Integer id){
        if(StringUtils.isBlank(projectName)){
            return Resp.fail("参数不能为空");
        }
        int flag = projectService.updateProject(projectName,id);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("修改失败");
        }
    }


    // @LogAnnotation(module = "回路导航",description = "根据项目Id查询信息")
    @PostMapping("/queryProjectByProId")
    @ApiOperation(value="根据项目Id查询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id",required = true,  dataType = "String",defaultValue = "50642475")
    })
    public Resp queryProjectByProId(String projectId){
        Project flag = projectService.queryProjectByProId(projectId);
            return Resp.success(flag);
    }


    @LogAnnotation(module = "回路导航",description = "根据id删除城市")
    @PostMapping("/deleteCityById")
    @ApiOperation(value="根据id删除城市")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id",  dataType = "Integer",defaultValue = "3")
    })
    public Resp deleteCityById(Integer id){
        City city=cityService.queryCityById(id);
        int count=projectService.selectCountByCityId(city.getCityId());
        if(count>0){
            return Resp.fail("删除失败，区域下还没项目未删除");
        }
        int flag = cityService.deleteCityById(id);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("删除失败");
        }
    }

    @LogAnnotation(module = "回路导航",description = "添加城市")
    @PostMapping("/addCity")
    @ApiOperation(value="添加城市")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cityName", value = "城市名称",required = true,  dataType = "String",defaultValue = "三门峡"),
            @ApiImplicitParam(paramType = "query", name = "cityCode", value = "城市编码",required = true,  dataType = "String")
    })
    public Resp addCity(String cityName,String cityCode){
        if(StringUtils.isBlank(cityName) || StringUtils.isBlank(cityCode)){
            return Resp.fail("参数不能为空");
        }
        List<City> city = cityService.queryCity();
        int sum=0;
        int sums=0;
        for (City citys : city) {
            if(citys.getCityCode().equals(cityCode)){
                sum=sum+1;
            }
            if(citys.getCityName().equals(cityName)){
                sums=sums+1;
            }
        }
        if(sum>0 || sums>0){
            return Resp.fail("编码或城市重复，请重新添加");
        }

        int flag = cityService.addCity(cityName,cityCode);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("添加失败");
        }
    }


    @LogAnnotation(module = "回路导航",description = "修改城市")
    @PostMapping("/updateCity")
    @ApiOperation(value="修改城市")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cityName", value = "城市名称",required = true,  dataType = "String",defaultValue = "三门峡"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer",defaultValue = "19")
    })
    public Resp updateCity(String cityName,Integer id){
        if(StringUtils.isBlank(cityName)){
            return Resp.fail("参数不能为空");
        }
        List<City> city = cityService.queryCity();
        int sums=0;
        for (City citys : city) {

            if(citys.getCityName().equals(cityName)){
                sums=sums+1;
            }
        }
        if(sums>0){
            return Resp.fail("城市重复，请重新添加");
        }

        int flag = cityService.updateCity(cityName,id);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("修改失败");
        }
    }


    //@LogAnnotation(module = "回路导航",description = "根据id查询城市")
    @PostMapping("/queryCityById")
    @ApiOperation(value="根据id查询城市")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer",defaultValue = "19")
    })
    public Resp queryCityById(Integer id){
        City flag = cityService.queryCityById(id);
            return Resp.success(flag);
    }

}
